﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Citiport.Util.Language;
using System.Collections;
using Citiport.Util.Google.Language;
    
/// <summary>
/// Class for managing user's geographical and language setting
/// Author: Jeff
/// Date: 04/04/2008
/// </summary>
public class GeoLingManager
{
    private HttpResponse _response;
    private HttpRequest _request;
    private CookiesManager _cookieMonster;
    public static Hashtable CultureInfoLang = new Hashtable();
    public static String DEFAULT_CULTURE_NAME = "en-US";


    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="userRequest">HttpRequest</param>
    /// <param name="serverResponse">HttpRespsonse</param>
	public GeoLingManager(HttpRequest userRequest, HttpResponse serverResponse)
	{
        _request = userRequest;
        _response = serverResponse;
        if (_request == null || _response == null)
        {
            HttpContext current = HttpContext.Current;
            _request = current.Request;
            _response = current.Response;
        }
        _cookieMonster = new CookiesManager(_request, _response);
	}

    /// <summary>
    /// static contructor
    /// </summary>
    static GeoLingManager()
    {
        CultureInfoLang.Add("zh", "zh-CN");
        CultureInfoLang.Add("zh-tw", "zh-CN");
        CultureInfoLang.Add("en", "en-US");
    }

    /// <summary>
    /// Return proper culture name for CultureInfo constructor
    /// </summary>
    /// <param name="lang"></param>
    /// <returns>Culture nam ex: en-US, zh-CN 
    /// refer to http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
    /// </returns>
    public static String CultutrInfoName(String lang)
    {
        if (lang == null)
            return DEFAULT_CULTURE_NAME;
        Object culName = CultureInfoLang[lang.Trim().ToLower()];
        if (culName == null)
            return DEFAULT_CULTURE_NAME;
        return DataUtil.ObjetToString(culName);
    }

    public String CultureName()
    {
        return CultutrInfoName(getLang());
    }


    /// <summary>
    /// Get language pref from cookie
    /// </summary>
    /// <returns>langugage code -en</returns>
    public String getLangCookie()
    {
        String lang_no =  _cookieMonster.getCookieValue(Citiport.Core.Const.COOKIE_USER_LANG);
        return lang_no;
    }

    /// <summary>
    /// Get user's language pref from db
    /// </summary>
    /// <returns>langugage code -en</returns>
    public String getLangUserSetting()
    {
        String lang_code;
        String user_no = _cookieMonster.getCookieValue(Citiport.Core.Const.COOKIE_USER_LANG);
        if (user_no == null)
            return null;
        ctpDataContext db = new ctpDataContext();
        var user = from p in db.users
                   join l in db.languages on p.prefer_language_no equals l.no
                   where p.no.ToString() == user_no
                   select new {lang_code = l.code };
        int userCount = user.Count();
        if (userCount < 1)
            return null;//throw exception
        else
            lang_code = user.First().lang_code;
        return lang_code;
    }

    /// <summary>
    /// Get Browser language
    /// </summary>
    /// <returns>langugage code -en</returns>
    public String getLangBrowserPref()
    {
        String lang_code = _request.UserLanguages[0];
        return lang_code;
    }

    /// <summary>
    /// Get user's language reference by the priority- user setting in db=>cookie=>broswer pref
    /// </summary>
    /// <returns>language code in 2 letters EX: en, zh, fr</returns>
    public String getLang()
    {
        String lang = getLangUserSetting();
        if (lang == null)
            lang = getLangCookie();
        if (lang == null)
            lang = getLangBrowserPref();
        return langCodeFilter(lang);
    }

    /// <summary>
    /// Filter the langugage code from en-US to en
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public String langCodeFilter(String code)
    {
        if (code.ToLower().Equals(Language.CHINESE_TRADITIONAL.ToLower()))
        {
            return code;
        }
        return code.Split('-')[0];
    }

    public int getTextLang(Hashtable lang_table, String text)
    {
        DatabaseUtil dbUtil = new DatabaseUtil();
        String code = GoogleLangAPI.DetectLang(text);
        Object result = lang_table[code.ToLower()];
        return DataUtil.OjectToInt(result);
    }
}
